package com.usian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.common.constants.AdminConstants;
import com.usian.feign.ArticleFeign;
import com.usian.feign.WemediaFeign;
import com.usian.mapper.ApUserRealnameMapper;
import com.usian.mapper.ApUsermapper;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.LinkedHashMap;

@Service
public class ApUserRealnameServiceImpl extends BaseServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Autowired
    private ApUsermapper apUsermapper;

    @Autowired
    private ArticleFeign articleFeign;

    @Autowired
    private WemediaFeign wemediaFeign;

    @Override
    public ResponseResult list(AuthDto dto) {
        if (dto == null || (dto.getStatus() != null && (dto.getStatus() != 0 && dto.getStatus() != 1 && dto.getStatus() != 2 && dto.getStatus() != 9))) {
            return paramError;
        }

        LambdaQueryWrapper<ApUserRealname> eq = Wrappers.<ApUserRealname>lambdaQuery().eq(ApUserRealname::getStatus, dto.getStatus() != null ? dto.getStatus() : 1);
        return selectFindAll(dto, eq);
    }


    @Override
    @GlobalTransactional
    public ResponseResult updateStatusById(AuthDto dto) {
        if (dto == null || dto.getStatus() == null || dto.getId() == null) return paramError;
        if (dto.getStatus() != 2 && dto.getStatus() != 9) return paramError;
        ApUserRealname apUserRealname = getById(dto.getId());
        try {
            BeanUtils.copyProperties(apUserRealname, dto);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        if (StringUtils.isNotEmpty(dto.getMsg())) apUserRealname.setReason(dto.getMsg());
        updateById(apUserRealname);

        if (dto.getStatus() == AdminConstants.PASS_AUTH) {
            ApUser apUser = apUsermapper.selectById(apUserRealname.getUserId());
            apUser.setFlag((short) 1);
            apUsermapper.updateById(apUser);

            //向自媒体表添加自媒体人
            WmUser wmUser = wemediaFeign.findByName(apUser.getName());
            if (wmUser == null) {
                wmUser = new WmUser();
                wmUser.setName(apUser.getName());
                wmUser.setPassword(apUser.getPassword());
                wmUser.setSalt(apUser.getSalt());
                wmUser.setPhone(apUser.getPhone());

                try {
                    BeanUtils.copyProperties(wmUser, apUser);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                wmUser.setApUserId(apUser.getId());
                wmUser.setStatus(9);
                wmUser.setType(1);
                wmUser.setCreatedTime(new Date());
                wmUser.setLoginTime(new Date());
                ResponseResult save = wemediaFeign.save(wmUser);
                LinkedHashMap map = (LinkedHashMap) save.getData();
                Integer id = (Integer) map.get("id");
                wmUser.setId(id);
            }
            //向作者表同步作者

            ApAuthor apAuthor = articleFeign.findByUserId(apUser.getId());
            if (apAuthor == null) {
                apAuthor = new ApAuthor();
                apAuthor.setName(apUser.getName());
                apAuthor.setType(2);
                apAuthor.setUserId(apUser.getId());
                apAuthor.setCreatedTime(new Date());
                apAuthor.setWmUserId(wmUser.getId());
                ResponseResult save = articleFeign.save(apAuthor);
                LinkedHashMap map = (LinkedHashMap) save.getData();
                Integer id = (Integer) map.get("id");
                wmUser.setApAuthorId(id);
                //更新自媒体表中的作者的Id
                wemediaFeign.updateAuthorId(wmUser);

            }
//            int i=1/0;
            return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS, "认证成功");
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS, "认证失败");

    }
}
